//数据表格的初始化与赋值
$(function() {
	var dataSrc = [];
	var myTable = $('#datalist').DataTable({
		// columns: colHeads,
		data: dataSrc,
		ordering: false,
		paging: false,
		searching: false,
		select: true,
		info: false,
		"footerCallback": function(tfoot, data, start, end, display) {
			var api = this.api();

			if (data.length == 0) {
				api.column(2).footer().innerHTML = '0.0'
				api.column(3).footer().innerHTML = '0.0%'
				return
			}

			let total = api.column(2).data().reduce(function(a, b) {
				return Number(a) + Number(b);
			}, 0)

			api.column(2).footer().innerHTML = total;

			// total = api.column(3).data().reduce(function(a, b) {
			// 	return Number(a) + Number(b);
			// }, 0)

			api.column(3).footer().innerHTML = (total / data.length).toFixed(1).toString() + '%';
		}
	})

	$('#a_custom').click(function() {
		let dataSrc = setAllDataToTabel();
		//myTable.reload();

		myTable.clear().rows.add(dataSrc).draw();
	})
})

function setAllDataToTabel() {
	if (!window.g_batch)
		return [];
	if (!window.g_batch.data)
		return [];

	let data = window.g_batch.data

	let arr = [];
	let i = 1;
	for (let obj of data) {
		if (obj.changeNum) {
			let aa = obj.changeNum.health
			arr.push([i++, obj.fileName, obj.changeNum.health, obj.changeNum.health.toString()])
		} else {
			let aa = obj.stat.health
			arr.push([i++, obj.fileName, obj.stat.health, obj.stat.health.toString()])
		}
	}
	return arr
}
